package com.sn.author;

import com.sn.author.monitor.WorkerManage;
import com.sn.author.redis.RedisUtil;
import com.sn.author.utils.Analysis;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.data.web.config.EnableSpringDataWebSupport;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.unit.DataSize;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.MultipartConfigElement;

@SpringBootApplication
@Slf4j
@MapperScan("com.sn.author.dao")
@EnableSwagger2
@EnableCaching
@EnableSpringDataWebSupport
@EnableTransactionManagement
@EnableAsync
public class AuthorApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthorApplication.class, args);
    }

    @Bean
    public DataLoader dataLoader() {
        return new DataLoader();
    }

    /**
     * 文件上传配置
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 文件最大
        factory.setMaxFileSize(DataSize.parse("10240000")); //KB,MB
        // 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.parse("10240000"));
        factory.setLocation("/data/tmp/");
        return factory.createMultipartConfig();
    }


    @Order
    static class DataLoader implements CommandLineRunner {

        @Override
        public void run(String... args) throws Exception {
            long start = System.currentTimeMillis();
            RedisUtil.initRedis();
            Class.forName("com.sn.author.monitor.WorkerManage");
            Class.forName("com.sn.author.utils.Analysis");
            log.info("System before component init successful. OverTime:[{}]",(System.currentTimeMillis() - start));
        }
    }

}
